package cn.com.ddapp.ddm.ui;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.target.ImageViewTargetFactory;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import butterknife.BindView;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.photopicker.Image;

public class ShareActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.tv_activit)
    TextView tv_activit;
    @BindView(R.id.tv_share)
    TextView tv_share;
    private Dialog dialog;
    String userId;
    UMImage umImage;

    @Override
    public void initView() {
        titletext.setText("邀请好友");
        back.setOnClickListener(this);
        tv_activit.setOnClickListener(this);
        tv_share.setOnClickListener(this);
        userId = SPUtils.getStringData(this,SPUtils.USERID);
        umImage = new UMImage(this,R.mipmap.appp);
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_share;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    public void onDestroy() {
        if(dialog!=null){
            dialog.dismiss();
        }

        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.tv_share:
                showDialog();
                break;
            case R.id.iv_exit:
                dialog.dismiss();
                break;
            //微信好友
            case R.id.wecat:
                onSharePlat(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.qq:
                onSharePlat(SHARE_MEDIA.QQ);
                break;
            //微信朋友圈
            case R.id.pengquan:
                onSharePlat(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.tv_activit:
                Bundle bundl2e=new Bundle();
                bundl2e.putString("category","3");
                startActivity(new Intent(this,AboutActivity.class).putExtra("ABOUT",bundl2e));
                break;
        }
        super.onClick(view);
    }
    UMShareListener umShareListener=new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            BaseApplication.showToast("成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            throwable.printStackTrace();
            Log.e("===",throwable.getMessage());
            BaseApplication.showToast("失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {

        }
    };
    public void onSharePlat(SHARE_MEDIA media){
       new ShareAction(this)
               .setPlatform(media)
               .setCallback(umShareListener)
               .withMedia(umImage)
               .withTitle("买烟花，送货上门啦！上亿指商城红包免费送，快抢！")
               .withText("上亿指商城买烟花，线上先看效果再下单，线下送货到家。进商城分享即有红包，领到手软！")
               .withTargetUrl("http://139.224.69.131:8080//user/share.api?userId="+userId)
               .share();
        dialog.dismiss();
    }

    protected void showDialog() {
        dialog = new Dialog(this);
        // 去对话框标题栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = dialog.getWindow();
        // 背景设置为透明色，去掉对话框的黑色背景框
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 布局参数
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM; // 设置对话框显示位置
        params.windowAnimations = R.style.MyDialogAnimation; // 对话框显示或隐藏时的动画效果
        params.dimAmount = 0.5f; // 对话框背景阴影效果，0到1， 表示透明度
        // 设置宽度填充父类：
        // 对话框布局文件根节点设置minWidth=100000dp
        dialog.setContentView(R.layout.layout_dialog_share);
        dialog.findViewById(R.id.pengquan).setOnClickListener(this);
        dialog.findViewById(R.id.wecat).setOnClickListener(this);
        dialog.findViewById(R.id.iv_exit).setOnClickListener(this);
        dialog.findViewById(R.id.qq).setOnClickListener(this);
        dialog.setCanceledOnTouchOutside(true);// 点击外部可以取消对话框
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
